<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Synchronized | Oliver知识收集站</title>
    <meta name="generator" content="VuePress 1.9.7">
    
    <meta name="description" content="享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css" as="style"><link rel="preload" href="/oliver-vuepress/assets/js/app.c21e6ffc.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/3.6dd9a2a1.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/1.898920d0.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/28.ce83b09c.js" as="script"><link rel="prefetch" href="/oliver-vuepress/assets/js/10.41b2bf91.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/11.a95c117d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/12.8607f0e1.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/13.a52d6846.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/14.249b4e52.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/15.d458d12e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/16.ba334206.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/17.1b91c9fa.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/18.e2ea2eb5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/19.bf0e2553.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/20.268bd174.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/21.cd1bbed5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/22.da4bc7f7.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/23.12f0c72f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/24.b7886742.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/25.6e71af85.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/26.5c127243.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/27.e98fd8bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/29.50398f0f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/30.05e1339c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/31.ef4b13fb.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/32.ba5f8351.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/33.3902db0a.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/34.36a05884.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/35.87215872.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/36.db360c58.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/37.402e5374.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/38.c9228dd8.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/39.72ba5d1f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/4.7bb03d47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/40.7e7949bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/41.c0d5b947.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/42.d9984467.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/43.e6a43668.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/44.10d7fe47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/45.f692ec2d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/46.9b920343.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/47.8e3d94f9.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/48.7d356e5b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/49.b0df6271.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/5.1fa544da.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/50.805e1466.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/51.1b31d40e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/52.44e69a41.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/53.da1def53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/54.6569f7db.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/55.5fc3de47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/56.da649377.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/57.6ff15ed4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/58.a62f6424.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/59.f68ae517.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/6.f5bd8e9b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/60.dda416bc.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/61.4e0c719f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/62.8c5ef01e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/63.7089eb8b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/64.b5ec150d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/65.6720cda4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/66.4ee90e29.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/67.cc4b0c6d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/7.d5950c53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/8.382fb3a5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/9.d593f4c1.js">
    <link rel="stylesheet" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Oliver知识收集站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/oliver-vuepress/" class="home-link router-link-active"><!----> <span class="site-name">Oliver知识收集站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link router-link-active"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><!----> <h3 class="name" data-v-39576ba9>
    oliver.shi
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>52</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link router-link-active"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>并发</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/oliver-vuepress/articles/java/concurrent/volatile.html" class="sidebar-link">volatile</a></li><li><a href="/oliver-vuepress/articles/java/concurrent/threadlocal.html" class="sidebar-link">threadLocal</a></li><li><a href="/oliver-vuepress/articles/java/concurrent/reentrantLock.html" class="sidebar-link">reentrantLock</a></li><li><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html" aria-current="page" class="active sidebar-link">synchronized</a></li><li><a href="/oliver-vuepress/articles/java/concurrent/executor.html" class="sidebar-link">线程池</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Synchronized</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">Synchronized</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>oliver.shi</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>10/22/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Java</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="简介"><a href="#简介" class="header-anchor">#</a> 简介</h2> <blockquote><p><code>Synchronized</code>   是 Java中的关键字，其目的：为了保证同一时刻只能被一个线程使用。
同时  <code>Synchronized</code>   作为一个 <code>内部锁</code> ，它保障原子性、可见性、有序性</p></blockquote> <p><strong><code>注</code></strong>  在 Java 早期版本中，synchronized 属于 <strong>重量级锁</strong>，效率低下。 常被人说【synchronized好用，简单，性能不差】</p> <h2 id="使用方式"><a href="#使用方式" class="header-anchor">#</a> 使用方式</h2> <p><code>Synchronized</code>  有三种使用方式：</p> <ul><li>修饰实例方法:   锁作用在 当前对象</li> <li>修饰静态方法： 锁作用在 当前对象的类</li> <li>修饰代码块:  可以指定锁在 对象  或  类</li></ul> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">class</span> <span class="token class-name">SynchronizedDemo</span> <span class="token punctuation">{</span>

    <span class="token comment">// 同步方法 - 给 当前对象加锁</span>
    <span class="token keyword">public</span> <span class="token keyword">synchronized</span> <span class="token keyword">void</span> <span class="token function">doMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 同步静态方法 给 对象的 class 类 进行加锁</span>
    <span class="token keyword">public</span> <span class="token keyword">synchronized</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">doMethod2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 同步代码块 可以指定 对象 或 类 进行加锁</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">doMethod3</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">synchronized</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">synchronized</span> <span class="token punctuation">(</span><span class="token class-name">SynchronizedDemo</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;hello world&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="锁实现原理"><a href="#锁实现原理" class="header-anchor">#</a> 锁实现原理</h2> <blockquote><p>通过 反编译后，可以看出 JVM 使用两种不同的方式实现同步操作
同步方法：  JVM 采用 <code>ACC_SYNCHRONIZED</code>  标识符来实现同步
同步代码块： JVM 采用 <code>monitorenter</code> 、 <code>monitorexit</code>  两个指令来实现同步</p></blockquote> <div class="language-java extra-class"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">synchronized</span> <span class="token keyword">void</span> <span class="token function">doMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  descriptor<span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token class-name">V</span>
  flags<span class="token operator">:</span> ACC_PUBLIC<span class="token punctuation">,</span> ACC_SYNCHRONIZED
  <span class="token class-name">Code</span><span class="token operator">:</span>
    stack<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> locals<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> args_size<span class="token operator">=</span><span class="token number">1</span>
       <span class="token number">0</span><span class="token operator">:</span> getstatic     #<span class="token number">2</span>                  <span class="token comment">// Field java/lang/System.out:Ljava/io/PrintStream;</span>
       <span class="token number">3</span><span class="token operator">:</span> ldc           #<span class="token number">3</span>                  <span class="token comment">// String hello world</span>
       <span class="token number">5</span><span class="token operator">:</span> invokevirtual #<span class="token number">4</span>                  <span class="token comment">// Method java/io/PrintStream.println:(Ljava/lang/String;)V</span>
       <span class="token number">8</span><span class="token operator">:</span> <span class="token keyword">return</span>
    <span class="token class-name">LineNumberTable</span><span class="token operator">:</span>
      line <span class="token number">5</span><span class="token operator">:</span> <span class="token number">0</span>
      line <span class="token number">6</span><span class="token operator">:</span> <span class="token number">8</span>

<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">synchronized</span> <span class="token keyword">void</span> <span class="token function">doMethod2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  descriptor<span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token class-name">V</span>
  flags<span class="token operator">:</span> ACC_PUBLIC<span class="token punctuation">,</span> ACC_STATIC<span class="token punctuation">,</span> ACC_SYNCHRONIZED
  <span class="token class-name">Code</span><span class="token operator">:</span>
    stack<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> locals<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> args_size<span class="token operator">=</span><span class="token number">0</span>
       <span class="token number">0</span><span class="token operator">:</span> getstatic     #<span class="token number">2</span>                  <span class="token comment">// Field java/lang/System.out:Ljava/io/PrintStream;</span>
       <span class="token number">3</span><span class="token operator">:</span> ldc           #<span class="token number">3</span>                  <span class="token comment">// String hello world</span>
       <span class="token number">5</span><span class="token operator">:</span> invokevirtual #<span class="token number">4</span>                  <span class="token comment">// Method java/io/PrintStream.println:(Ljava/lang/String;)V</span>
       <span class="token number">8</span><span class="token operator">:</span> <span class="token keyword">return</span>
    <span class="token class-name">LineNumberTable</span><span class="token operator">:</span>
      line <span class="token number">10</span><span class="token operator">:</span> <span class="token number">0</span>
      line <span class="token number">11</span><span class="token operator">:</span> <span class="token number">8</span>

<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">doMethod3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  descriptor<span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token class-name">V</span>
  flags<span class="token operator">:</span> ACC_PUBLIC
  <span class="token class-name">Code</span><span class="token operator">:</span>
    stack<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> locals<span class="token operator">=</span><span class="token number">4</span><span class="token punctuation">,</span> args_size<span class="token operator">=</span><span class="token number">1</span>
       <span class="token number">0</span><span class="token operator">:</span> aload_0
       <span class="token number">1</span><span class="token operator">:</span> dup
       <span class="token number">2</span><span class="token operator">:</span> astore_1
       <span class="token number">3</span><span class="token operator">:</span> monitorenter
       <span class="token number">4</span><span class="token operator">:</span> getstatic     #<span class="token number">2</span>                  <span class="token comment">// Field java/lang/System.out:Ljava/io/PrintStream;</span>
       <span class="token number">7</span><span class="token operator">:</span> ldc           #<span class="token number">3</span>                  <span class="token comment">// String hello world</span>
       <span class="token number">9</span><span class="token operator">:</span> invokevirtual #<span class="token number">4</span>                  <span class="token comment">// Method java/io/PrintStream.println:(Ljava/lang/String;)V</span>
      <span class="token number">12</span><span class="token operator">:</span> aload_1
      <span class="token number">13</span><span class="token operator">:</span> monitorexit
      <span class="token number">14</span><span class="token operator">:</span> <span class="token keyword">goto</span>          <span class="token number">22</span>
      <span class="token number">17</span><span class="token operator">:</span> astore_2
      <span class="token number">18</span><span class="token operator">:</span> aload_1
      <span class="token number">19</span><span class="token operator">:</span> monitorexit
      <span class="token number">20</span><span class="token operator">:</span> aload_2
      <span class="token number">21</span><span class="token operator">:</span> athrow
      <span class="token number">22</span><span class="token operator">:</span> ldc           #<span class="token number">5</span>                  <span class="token comment">// class SynchronizedDemo</span>
      <span class="token number">24</span><span class="token operator">:</span> dup
      <span class="token number">25</span><span class="token operator">:</span> astore_1
      <span class="token number">26</span><span class="token operator">:</span> monitorenter
      <span class="token number">27</span><span class="token operator">:</span> getstatic     #<span class="token number">2</span>                  <span class="token comment">// Field java/lang/System.out:Ljava/io/PrintStream;</span>
      <span class="token number">30</span><span class="token operator">:</span> ldc           #<span class="token number">3</span>                  <span class="token comment">// String hello world</span>
      <span class="token number">32</span><span class="token operator">:</span> invokevirtual #<span class="token number">4</span>                  <span class="token comment">// Method java/io/PrintStream.println:(Ljava/lang/String;)V</span>
      <span class="token number">35</span><span class="token operator">:</span> aload_1
      <span class="token number">36</span><span class="token operator">:</span> monitorexit
      <span class="token number">37</span><span class="token operator">:</span> <span class="token keyword">goto</span>          <span class="token number">45</span>
      <span class="token number">40</span><span class="token operator">:</span> astore_3
      <span class="token number">41</span><span class="token operator">:</span> aload_1
      <span class="token number">42</span><span class="token operator">:</span> monitorexit
      <span class="token number">43</span><span class="token operator">:</span> aload_3
      <span class="token number">44</span><span class="token operator">:</span> athrow
      <span class="token number">45</span><span class="token operator">:</span> <span class="token keyword">return</span>

</code></pre></div><p><strong><code>注</code></strong>   无论是 ACC_SYNCHRONIZED 还是 monitorenter、monitorexit都是基于Monitor实现的，在Java虚拟机(HotSpot)中，Monitor是基于C++实现的，由ObjectMonitor实现。</p> <h3 id="monitor-主要数据结构如下"><a href="#monitor-主要数据结构如下" class="header-anchor">#</a> Monitor 主要数据结构如下：</h3> <div class="language-java extra-class"><pre class="language-java"><code><span class="token class-name">ObjectMonitor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  _header       <span class="token operator">=</span> NULL<span class="token punctuation">;</span>
  _count        <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>    <span class="token comment">// 记录个数</span>
  _waiters      <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span>
  _recursions   <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>     <span class="token comment">// 线程的重入次数</span>
  _object       <span class="token operator">=</span> NULL<span class="token punctuation">;</span>  <span class="token comment">// 存储 Monitor 对象</span>
  _owner        <span class="token operator">=</span> NULL<span class="token punctuation">;</span>  <span class="token comment">// 持有锁的 当前线程</span>
  _WaitSet      <span class="token operator">=</span> NULL<span class="token punctuation">;</span>  <span class="token comment">// 处于 wait 状态的线程，会被加入到 _WaitSet</span>
  _WaitSetLock  <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span>
  _Responsible  <span class="token operator">=</span> NULL <span class="token punctuation">;</span>
  _succ         <span class="token operator">=</span> NULL <span class="token punctuation">;</span>
  _cxq          <span class="token operator">=</span> NULL <span class="token punctuation">;</span>   <span class="token comment">// 单向列表</span>
  <span class="token class-name">FreeNext</span>      <span class="token operator">=</span> NULL <span class="token punctuation">;</span>
  _EntryList    <span class="token operator">=</span> NULL <span class="token punctuation">;</span> <span class="token comment">// 处于等待锁 block 状态的线程，会被加入到该列表</span>
  _SpinFreq     <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span>
  _SpinClock    <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span>
  <span class="token class-name">OwnerIsThread</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span>
  _previous_owner_tid <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="同步方法-什么时候获取锁"><a href="#同步方法-什么时候获取锁" class="header-anchor">#</a> 同步方法 什么时候获取锁</h3> <blockquote><p>检测 是否 有 <code>ACC_SYNCHRONIZED</code> 标识,需要先获取监视器锁，然后在开始执行方法，方法执行完成后在释放监视器锁</p></blockquote> <p><strong><code>注</code></strong>  其他线程来请求执行时，无法获得监视器锁而被阻断</p> <h3 id="同步方法-什么时候释放锁"><a href="#同步方法-什么时候释放锁" class="header-anchor">#</a> 同步方法 什么时候释放锁：</h3> <ol><li>方法执行完成后在释放监视器锁</li> <li>当出现异常时，方法内没有处理异常，就会在抛出异常之前，监视器锁会被释放</li></ol> <h3 id="同步代码块-什么时候获取锁-什么时候释放锁"><a href="#同步代码块-什么时候获取锁-什么时候释放锁" class="header-anchor">#</a> 同步代码块 什么时候获取锁？什么时候释放锁？</h3> <ul><li>monitorenter ： 指令理解为加锁</li> <li>monitorexit:        指令理解为解锁</li></ul> <p>原理：</p> <ul><li>每个对象自身维护这一个加锁的计数器</li> <li>当线程 获取到锁 则对计数器 +1  （同一个线程可以多次获取该对象的锁）</li> <li>当线程 释放锁 则对计数器 -1</li> <li>当计数器 自减到 0 是，锁将被释放，其他线程可以进行获取锁</li></ul> <p><a href="https://www.hollischuang.com/archives/2030" target="_blank" rel="noopener noreferrer">Moniter 实现原理<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="三特性"><a href="#三特性" class="header-anchor">#</a> 三特性</h2> <h3 id="原子性"><a href="#原子性" class="header-anchor">#</a> 原子性</h3> <blockquote><p>一个操作不可中断的，要么全部执行完成、要么就都不执行</p></blockquote> <p><code>Synchronized</code>   通过 <code>monitorenter</code> 和 <code>monitorexit</code>指令，可以保证被synchronized修饰的代码在同一时间只能被一个线程访问，在锁未释放之前，无法被其他线程访问到。</p> <p><strong><code>例</code></strong></p> <blockquote><p>线程1在执行<code>monitorenter</code>指令的时候，会对<code>Monitor</code>进行加锁，加锁后其他线程无法获得锁，除非线程1主动解锁。即使在执行过程中，由于某种原因，比如CPU时间片用完，线程1放弃了CPU，但是，他并没有进行解锁。而由于<code>synchronized</code> 的锁是可重入的，下一个时间片还是只能被他自己获取到，还是会继续执行代码。直到所有代码执行完。这就保证了原子性</p></blockquote> <h3 id="可见性"><a href="#可见性" class="header-anchor">#</a> 可见性</h3> <blockquote><p>当多个线程访问同一个变量时，一个线程修改了这个变量的值，其他线程能够立即看得到修改的值</p></blockquote> <p>synchronized又是如何保障可见性的呢？</p> <p><strong><code>核心</code></strong> <code>Synchronized</code>  关键字锁住的是对象，其值是具有可见性的</p> <ul><li>JAVA内存模型：
<ul><li>规定了所有的变量存储在主内存中，每个线程都要维护一个工作内存，使用变量时，会将主内存变量拷贝到工作内存中，改变变量值，也是对工作内存中进行改变。在不主动 与 主内存之间进行数据同步时，其他线程对变量的值是不可见的</li></ul></li> <li>Synchronized 执行机制 ：
<ul><li>为了保证可见性， 在释放锁之前，必须先把变量同步到主内存中，后续的线程访问到的就是被修改后的值</li></ul></li></ul> <h3 id="有序性"><a href="#有序性" class="header-anchor">#</a> 有序性</h3> <blockquote><p>程序执行的顺序按照代码的先后顺序执行</p></blockquote> <p><strong><code>注</code></strong> <code>Synchroized</code>  是  无法像 <code>volatile</code>  通过使用 <code>内存屏障</code> 进行 禁止指令重排 和 处理器优化的</p> <p><strong>为什么还说synchronized也提供了有序性保证呢？</strong></p> <ul><li>基于 <code>as-if-serial</code>  语义 :  保证了 单线程中 指令重排是有一定的限制的，只要编译器和处理器遵循这个语义，那么就可以任务单线程执行时 按照顺序执行的</li> <li>基于 <code>synchronized</code>  修饰的代码是 <code>原子性</code> 的，同一时间只能被同一个线程访问，也就是 <code>单线程</code> 执行</li></ul> <h2 id="锁优化"><a href="#锁优化" class="header-anchor">#</a> 锁优化</h2> <ul><li>Java 1.6 之前 <code>Synchronized</code>  只存在，无锁、重量锁</li> <li>Java 1.6 之后 对其进行了锁的优化 无锁、偏向锁、轻量锁、重量锁</li></ul> <h3 id="java对象头"><a href="#java对象头" class="header-anchor">#</a> Java对象头</h3> <p>JDK1.6 JVM 对象实现堆内存分为三部分：</p> <ul><li>对象头</li> <li>实例数据</li> <li>对其填充</li></ul> <p>对象头中有分为三部分：</p> <ul><li>Mark Word
<ul><li>Mark Word 记录了对象和锁有关的信息</li></ul></li> <li>指向类的指针</li> <li>数组长度</li></ul> <img src="/oliver-vuepress/java/concurrent/synchronized/1.png" alt="foo"> <img src="/oliver-vuepress/java/concurrent/synchronized/2.png" alt="foo"> <p><strong><code>核心</code></strong>   锁升级主要依赖于 Mark Word中的 锁标志位 和 释放偏向锁的标志位</p> <h3 id="无锁"><a href="#无锁" class="header-anchor">#</a> 无锁</h3> <blockquote><p>默认状态是无锁的。无锁没有对资源进行锁定，所有的线程都能访问并修改同一个资源，但同时只有一个线程能修改成功。 即 没用 <code>Synchornized</code>  修饰的代码块都是无锁的</p></blockquote> <h3 id="偏向锁"><a href="#偏向锁" class="header-anchor">#</a> 偏向锁</h3> <blockquote><p>当没有竞争出现时，默认会使用偏向锁。JVM 会利用 CAS 操作，在对象头上的 Mark Word 部分设置&gt; 线程 ID以表示这个对象偏向于当前线程，所以并不涉及真正的互斥锁。这样做的假设是基于在很多应用场景中，大部分对象生命周期中最多会被一个线程锁定，使用偏斜锁可以降低无竞争开销</p></blockquote> <img src="/oliver-vuepress/java/concurrent/synchronized/3.png" alt="foo"> <ul><li>升级的过程： 进程进入同步代码块时，会利用 CAS 将 Markwork 进行标识
<ul><li>线程ID</li> <li>锁状态标识：01</li> <li>是否偏向： 1</li></ul></li> <li>膨胀过程：当有另外线程进入，偏向锁会升级成轻量级锁。比如线程A是偏向锁，这是B线程进入，就会成轻量级锁，<strong>只要有两个线程就会升级成轻量级锁</strong></li></ul> <h3 id="轻量级锁"><a href="#轻量级锁" class="header-anchor">#</a> 轻量级锁</h3> <blockquote><p>多个线程竞争同步资源时，没有获取资源的线程自旋等待锁释放</p></blockquote> <ul><li>升级过程：在线程运行获取锁后，会在栈帧中创造锁记录并将MarkWord复制到锁记录，然后将MarkWord指向锁记录，如果当前线程持有锁，其他线程再进入，此时其他线程会cas自旋，直到获取锁，轻量级锁适合多线程交替执行，效率高（cas只消耗cpu，我在cas原理一篇文章中详细讲过。）。</li> <li>膨胀过程：有两种情况会膨胀成重量级锁。
<ul><li>1、cas自旋10次还没获取锁。</li> <li>2、其他线程正在cas获取锁，第三个线程竞争获取锁，锁也会膨胀变成重量级锁。</li></ul></li></ul> <h3 id="重量级锁"><a href="#重量级锁" class="header-anchor">#</a> 重量级锁</h3> <ul><li>重量锁</li></ul> <img src="/oliver-vuepress/java/concurrent/synchronized/4.png" alt="foo"> <img src="/oliver-vuepress/java/concurrent/synchronized/5.png" alt="foo"> <h2 id="锁消除"><a href="#锁消除" class="header-anchor">#</a> 锁消除</h2> <blockquote><p>在使用synchronized的时候，如果JIT经过逃逸分析之后发现并无线程安全问题的话，就会做锁消除。</p></blockquote> <p>锁消除：</p> <ul><li>逃逸分析：即: JIT 比编译器 借助 逃逸分析 来判断同步快所使用的锁对象是否只能够被一个线程访问</li> <li>如果是的话 JIT编译器 在编译这个同步代码块的时候就会取消这部分代码的同步</li></ul> <p><strong><code>例</code></strong></p> <div class="language-java extra-class"><pre class="language-java"><code><span class="token comment">// 优化前</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">Object</span> oliver <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">synchronized</span><span class="token punctuation">(</span>oliver<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>oliver<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 优化后</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">Object</span> oliver <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>oliver<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="锁粗化"><a href="#锁粗化" class="header-anchor">#</a> 锁粗化</h2> <ul><li>如果在一段代码中连续的对同一个对象反复加锁解锁，其实是相对耗费资源的，这种情况可以适当放宽加锁的范围，减少性能消耗。</li> <li>当JIT发现一系列连续的操作都对同一个对象反复加锁和解锁，甚至加锁操作出现在循环体中的时候，会将加锁同步的范围扩散（粗化）到整个操作序列的外部。</li></ul> <div class="language-java extra-class"><pre class="language-java"><code># 粗化前
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">100000</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>  
    <span class="token keyword">synchronized</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">{</span>  
        <span class="token keyword">do</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
<span class="token punctuation">}</span>  

# 粗化后    
  <span class="token keyword">synchronized</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">100000</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>  
       <span class="token keyword">do</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="疑问点"><a href="#疑问点" class="header-anchor">#</a> 疑问点：</h2> <ul><li>锁降级</li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">4/21/2022, 8:17:59 AM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/oliver-vuepress/articles/java/concurrent/reentrantLock.html" class="prev">
            reentrantLock
          </a></span> <span class="next"><a href="/oliver-vuepress/articles/java/concurrent/executor.html">
            线程池
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#简介" class="sidebar-link reco-side-简介" data-v-cb1513f6>简介</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#使用方式" class="sidebar-link reco-side-使用方式" data-v-cb1513f6>使用方式</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#锁实现原理" class="sidebar-link reco-side-锁实现原理" data-v-cb1513f6>锁实现原理</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#monitor-主要数据结构如下" class="sidebar-link reco-side-monitor-主要数据结构如下" data-v-cb1513f6>Monitor 主要数据结构如下：</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#同步方法-什么时候获取锁" class="sidebar-link reco-side-同步方法-什么时候获取锁" data-v-cb1513f6>同步方法 什么时候获取锁</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#同步方法-什么时候释放锁" class="sidebar-link reco-side-同步方法-什么时候释放锁" data-v-cb1513f6>同步方法 什么时候释放锁：</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#同步代码块-什么时候获取锁-什么时候释放锁" class="sidebar-link reco-side-同步代码块-什么时候获取锁-什么时候释放锁" data-v-cb1513f6>同步代码块 什么时候获取锁？什么时候释放锁？</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#三特性" class="sidebar-link reco-side-三特性" data-v-cb1513f6>三特性</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#原子性" class="sidebar-link reco-side-原子性" data-v-cb1513f6>原子性</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#可见性" class="sidebar-link reco-side-可见性" data-v-cb1513f6>可见性</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#有序性" class="sidebar-link reco-side-有序性" data-v-cb1513f6>有序性</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#锁优化" class="sidebar-link reco-side-锁优化" data-v-cb1513f6>锁优化</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#java对象头" class="sidebar-link reco-side-java对象头" data-v-cb1513f6>Java对象头</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#无锁" class="sidebar-link reco-side-无锁" data-v-cb1513f6>无锁</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#偏向锁" class="sidebar-link reco-side-偏向锁" data-v-cb1513f6>偏向锁</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#轻量级锁" class="sidebar-link reco-side-轻量级锁" data-v-cb1513f6>轻量级锁</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#重量级锁" class="sidebar-link reco-side-重量级锁" data-v-cb1513f6>重量级锁</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#锁消除" class="sidebar-link reco-side-锁消除" data-v-cb1513f6>锁消除</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#锁粗化" class="sidebar-link reco-side-锁粗化" data-v-cb1513f6>锁粗化</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/concurrent/synchronized.html#疑问点" class="sidebar-link reco-side-疑问点" data-v-cb1513f6>疑问点：</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/oliver-vuepress/assets/js/app.c21e6ffc.js" defer></script><script src="/oliver-vuepress/assets/js/3.6dd9a2a1.js" defer></script><script src="/oliver-vuepress/assets/js/1.898920d0.js" defer></script><script src="/oliver-vuepress/assets/js/28.ce83b09c.js" defer></script>
  </body>
</html>
